export const WeChatUtil = {
  isWechat() {
    return window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) + '' === 'micromessenger'
  },
  getToken() {
    let token = JSON.parse(window.localStorage.getItem('group_token'))
    return token
  },
  hasToken() {
    return localStorage.hasOwnProperty('group_token')
  },
  changeTitle(title) {
    document.title = title
    let i = document.createElement('iframe')
    i.src = ''
    i.style.display = 'none'
    i.onload = function () {
      setTimeout(function () {
        i.remove()
      }, 9)
    }
    document.body.appendChild(i)
  },
  getQueryString(letiable) {
    let query = window.location.href.split('?')
    if (query.length > 1) {
      let lets = query[1].split('&')
      for (let i = 0; i < lets.length; i++) {
        let pair = lets[i].split('=')
        if (pair[0] == letiable) {
          return pair[1]
        }
      }
    }
    return false
  },
  login(redirectPath) {
    if (redirectPath) {
      window.localStorage.setItem('redirectPath', redirectPath)
    }
    let channelId = this.getQueryString('channelId') ? this.getQueryString('channelId') : 0
    let loginCallbackUrl = window.location.origin + '/#/wechat/loginCallback'
    let domains = window.location.host.split('.')
    domains[0] = 'wechat'
    let res = '//' + domains.join('.') +
      '/api/wechat/authorize?scope=snsapi_userinfo&redirectUrl=' +
      encodeURIComponent(loginCallbackUrl) +
      `&channelId=${channelId}`
    window.location.href = res
  },
  initJsSdk(that, jsApiList, cb, pageContentId) {
    let params = {
      url: window.location.href.split('#')[0]
    }
    if (jsApiList && jsApiList.toString().indexOf('chooseWXPay') !== -1) {
      params.pay = true
    }
    kpiGetJsApiConfig(params, that).then(
      res => {
        jsApiList.push('onMenuShareTimeline')
        jsApiList.push('onMenuShareAppMessage')
        jsApiList.push('chooseImage')
        let config = Object.assign({ debug: false, jsApiList }, res.data.data)
        wx.config(config)
        wx.ready(() => {
          try {
            if (pageContentId !== 0) {
              let params = {
                pageContentId: pageContentId
              }
              kpiGetImageLink(params, that).then(res => {
                let pageContent = res.data.data
                let image = pageContent.imageLinks[0]
                wx.onMenuShareTimeline({
                  title: pageContent.pageContentName,
                  imgUrl: image.imageUrl,
                  link: image.targetUrl
                })
                wx.onMenuShareAppMessage({
                  title: pageContent.pageContentName,
                  imgUrl: image.imageUrl,
                  link: image.targetUrl,
                  desc: image.description
                })
              })
            }
            if (typeof cb === 'function') {
              cb()
            }
          } catch (e) {
            console.error(e)
          }
        })
      },
      err => {
        console.error(err)
      }
    )
    return wx
  }
}

/**
 * 判断值是否为空(undefined, null, 长度为零)
 * return true 值不为空,反之
 */
export function isExistence(value) {
  if (Object.prototype.toString.call(value) === '[object Array]') {
    return !Object.is(JSON.stringify(value), JSON.stringify([]))
  } else if (Object.prototype.toString.call(value) === '[object Object]') {
    return !Object.is(JSON.stringify(value), JSON.stringify({}))
  } else if (Object.prototype.toString.call(value) === '[object String]') {
    return value !== '' && value !== 'null'
  } else if (Object.prototype.toString.call(value) === '[object Undefined]') {
    return false
  } else if (Object.prototype.toString.call(value) === '[object Null]') {
    return false
  } else {
    return true
  }
}
